package com.example.bookmanager.AspectDemo;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;

@Aspect
@Component
@Slf4j
public class TimeAspect {
    @Around("execution(* com.example.bookmanager.Controller.*.*(..))")
    public Object recordTime(final ProceedingJoinPoint pjp) throws Throwable {
        //记录开始时间
        long startTime = System.currentTimeMillis();
        //执行原始方法
        Object result = pjp.proceed();
        //记录结束时间
        long endTime = System.currentTimeMillis();
        //打印日志
        log.info(pjp.getSignature().getName() + "用时: " + (endTime - startTime)+"ms");
        return result;
    }
}
